/**
 * DemoDao.java
 * @author xsm
 * date 2012-8-8
 */

package com.netistate.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.netistate.base.BaseDao;
import com.netistate.pojo.Demo;

@Repository
public class DemoDao extends BaseDao<Demo, Integer>{
	
	/**
	 * 根据id去找demo
	 * @param demo
	 * @return
	 */
	public Demo findAll(Demo demo){
		String hql = "from Demo where id=?";
		Query query = getSession().createQuery(hql);		
		return (Demo) query.list();
	}

	/**
	 * 根据orders排序
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Integer> findAllDemoByOrders() {
		String hql = "SELECT DISTINCT d.orders from Demo d";
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	/**
	 * 默认查询所有的demo,只显示最新的那条
	 * @param i 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Demo> findAllDemo(int i) {
		
		String hql = "FROM Demo WHERE orders=? ORDER BY createtime DESC";
		Query query = getSession().createQuery(hql);
		query.setInteger(0, i);
		return query.list();
	}

	/**
	 * 根据orders,查询某个Demo的全部信息
	 * @param o
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Demo> findOneDemoByOrdes(int orders) {
		
		String hql = "from Demo where orders = ? order by createtime desc";
		
		Query query = getSession().createQuery(hql);
		query.setParameter(0, orders);
		
		return query.list();
	}

	/**
	 * 点击添加项目服务表时，选择POC,勾选显示相关的demo信息时，就拿pid去找demo
	 * @param id
	 * @return
	 */
	public List<Demo> findDemoByPid(int id) {
		String hql = "from Demo where projectid = ? order by createtime desc";
		Query query = getSession().createQuery(hql);
		query.setInteger(0, id);
		return query.list();
	}

}
